package com.fyh.controller;

import com.fyh.constant.MessageConstant;
import com.fyh.entity.common.Result;
import com.fyh.entity.job.JobPositionRequest;
import com.fyh.entity.job.SimpleJobPosition;
import com.fyh.entity.user.Vitae;
import com.fyh.service.CandidateService;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping("/candidate")
public class CandidateController {

    @Autowired
    CandidateService candidateService;

    /**
     * 插入用户简历
     * @param vitae
     * @return
     */
    @PostMapping("/insertVitae")
    public Result insertVitae(@RequestBody Vitae vitae){
        log.info("插入用户简历:{}", vitae);
        candidateService.insertVitae(vitae);
        return Result.success();
    }

    /**
     * 上传简历文件
     * @param file
     * @return
     */
    @PostMapping("/uploadResumeFile")
    public Result<String> uploadResumeFile(@RequestParam("file") MultipartFile file){
        log.info("上传简历文件");
        String resumeFilePath = candidateService.uploadResumeFile(file);
        if(resumeFilePath == null){
            return Result.error(MessageConstant.UPLOAD_FILE_ERROR);
        }
        log.info("上传简历文件成功:{}", resumeFilePath);
        return Result.success(resumeFilePath);
    }

    /**
     * 更新用户简历
     * @param vitae
     * @return
     */
    @PostMapping("/updateVitae")
    public Result updateVitae(@RequestBody Vitae vitae){
        log.info("更新用户简历:{}", vitae);
        candidateService.updateVitae(vitae);
        return Result.success();
    }

    /**
     * 获取用户简历
     * @param userId
     * @return
     */
    @GetMapping("/getUserVitae")
    public Result<Vitae> getUserVitae(@RequestParam("userId") Long userId){
        log.info("获取用户简历:{}", userId);
        Vitae vitae = candidateService.getUserVitae(userId);
        return Result.success(vitae);
    }

    /**
     * 获取用户技能标签
     */
    @GetMapping("/getTagNameListByUserId")
    public Result<List<String>> getTagNameListByUserId(@RequestParam("userId") Long userId){
        log.info("获取用户技能标签:{}", userId);
        List<String> tagNameList = candidateService.getTagNameListByUserId(userId);
        return Result.success(tagNameList);
    }

    /**
     * 获取用户收藏职位状态
     * @param positionId
     * @param userId
     * @return
     */
    @GetMapping("/getCollectExist")
    public Result getCollectExist(@RequestParam("positionId") Long positionId, @RequestHeader("userId") Long userId){
        log.info("获取收藏职位状态:{}", positionId);
        return Result.success(candidateService.getCollectExist(positionId, userId));
    }

    /**
     * 收藏职位
     * @param positionId
     * @return
     */
    @PutMapping("/collectJob")
    public Result collectJob(@RequestParam("positionId") Long positionId, @RequestHeader("userId") Long userId){
        log.info("收藏职位:{}", positionId);
        candidateService.collectJob(positionId, userId);
        return Result.success();
    }

    /**
     * 取消收藏职位
     * @param positionId
     * @return
     */
    @PutMapping("/cancelCollectJob")
    public Result cancelCollectJob(@RequestParam("positionId") Long positionId, @RequestHeader("userId") Long userId){
        log.info("取消收藏职位:{}", positionId);
        candidateService.cancelCollectJob(positionId, userId);
        return Result.success();
    }

    /**
     * 获取收藏职位列表
     * @param userId
     * @return
     */
    @GetMapping("/getCollectSimpleJobList")
    public Result<List<SimpleJobPosition>> getCollectSimpleJobList(@RequestHeader("userId") Long userId){
        log.info("用户{}获取收藏的职位列表", userId);
        List<SimpleJobPosition> simpleJobPositionList = candidateService.getCollectSimpleJobList(userId);
        return Result.success(simpleJobPositionList);
    }

    /**
     * 查看简历是否投递
     * @param positionId
     * @param userId
     * @return
     */
    @GetMapping("/getDeliveryExist")
    public Result<Boolean> getDeliveryExist(@RequestParam("positionId") Long positionId, @RequestHeader("userId") Long userId){
        log.info("{}获取简历是否投递:{}", userId, positionId);
        Boolean exist = candidateService.getDeliveryExist(positionId, userId);
        System.out.println("是否投递：" + exist);
        return Result.success(exist);
    }

    /**
     * 投递简历
     * @param positionId
     * @param userId
     * @return
     */
    @PutMapping("/deliveryVitae")
    public Result deliveryVitae(@RequestParam("positionId") Long positionId, @RequestHeader("userId") Long userId){
        log.info("投递简历:{}", positionId);
        candidateService.deliveryVitae(positionId, userId);
        return Result.success();
    }
}
